Imports System.Windows.Forms
Imports System.Drawing

'Summary Box
'Created by Stuart Zahn
'theosof@theosoftware.com
'www.Theosoftware.com
'www.DivideTheWord.org
'Version 1.0
'Date Modified 6/7/06

'Copyright 2006 Stuart Zahn
'This control is freeware

Public Class Sumbox
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        SetStyle(ControlStyles.UserPaint, True)
        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.DoubleBuffer, True)
        SetStyle(ControlStyles.ResizeRedraw, True)

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents YScroll As System.Windows.Forms.VScrollBar
    Friend WithEvents pnlList As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.YScroll = New System.Windows.Forms.VScrollBar
        Me.pnlList = New System.Windows.Forms.Panel
        Me.SuspendLayout()
        '
        'YScroll
        '
        Me.YScroll.Dock = System.Windows.Forms.DockStyle.Right
        Me.YScroll.Location = New System.Drawing.Point(264, 0)
        Me.YScroll.Name = "YScroll"
        Me.YScroll.Size = New System.Drawing.Size(16, 264)
        Me.YScroll.TabIndex = 0
        '
        'pnlList
        '
        Me.pnlList.BackColor = System.Drawing.SystemColors.Window
        Me.pnlList.Dock = System.Windows.Forms.DockStyle.Fill
        Me.pnlList.ForeColor = System.Drawing.SystemColors.WindowText
        Me.pnlList.Location = New System.Drawing.Point(0, 0)
        Me.pnlList.Name = "pnlList"
        Me.pnlList.Size = New System.Drawing.Size(264, 264)
        Me.pnlList.TabIndex = 1
        '
        'Sumbox
        '
        Me.Controls.Add(Me.pnlList)
        Me.Controls.Add(Me.YScroll)
        Me.Name = "Sumbox"
        Me.Size = New System.Drawing.Size(280, 264)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Public Items As New SortedList
    Public SelectedIndex As Integer = -1

    Private RealItemPosition() As Integer
    Private RealItemHeight() As Integer
    Private ItemVerticalSpacing As Integer = 5

    Private MousePosY As Integer
    Private MaxY As Integer

    Public Sub CalculateItems()
        Try
            If Me.Visible Then
                If Items.Count = 0 Then
                    RealItemPosition = Nothing
                    RealItemHeight = Nothing
                    Enabled = False
                Else
                    Enabled = True
                    Dim ItemY As Integer = 0
                    ReDim RealItemPosition(Me.Items.Count - 1)
                    ReDim RealItemHeight(Me.Items.Count - 1)
                    For i As Integer = 0 To Me.Items.Count - 1
                        Dim f As New StringFormat(StringFormatFlags.FitBlackBox)
                        Dim ItemSize As SizeF = CreateGraphics.MeasureString(CStr(Me.Items.GetValueList(i)), Me.Font, pnlList.Width - 5, f)
                        RealItemHeight(i) = ItemSize.Height
                        RealItemPosition(i) = ItemY
                        ItemY += RealItemHeight(i) + ItemVerticalSpacing
                    Next
                    MaxY = (ItemY - Height) + 10
                    If MaxY < 0 Then
                        MaxY = 0
                        YScroll.Enabled = False
                    Else
                        YScroll.Enabled = True
                        YScroll.Minimum = 0
                        YScroll.Maximum = MaxY
                    End If
                End If
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message & ex.StackTrace)
        End Try
    End Sub


    Private Sub pnlList_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pnlList.Paint
        Try
            e.Graphics.Clear(SystemColors.Window)
            If Items.Count > 0 Then
                Dim BackgroundColor As Brush, TextColor As Brush
                Dim fs As New FontStyle
                Dim f As New StringFormat(StringFormatFlags.FitBlackBox)
                For i As Integer = 0 To Me.Items.Count - 1
                    Try
                        Dim lr As New RectangleF(0, RealItemPosition(i) - YScroll.Value, pnlList.Width - 5, RealItemHeight(i))
                        If Me.SelectedIndex = i Then
                            BackgroundColor = SystemBrushes.Highlight
                            TextColor = SystemBrushes.HighlightText
                            fs = FontStyle.Regular
                        Else
                            TextColor = SystemBrushes.WindowText
                            fs = FontStyle.Regular
                            If i Mod 2 = 0 Then
                                BackgroundColor = New SolidBrush(Color.FromArgb(SystemColors.Window.R * 0.9, SystemColors.Window.G * 0.9, SystemColors.Window.B * 0.95))
                            Else
                                BackgroundColor = SystemBrushes.Window
                            End If
                        End If
                        e.Graphics.FillRectangle(BackgroundColor, lr)
                        e.Graphics.DrawString(CStr(Me.Items.GetValueList(i)), New Font(Font.Name, Font.Size, fs), TextColor, lr, f)
                    Catch ex As Exception
                        Console.WriteLine(ex.Message & ex.StackTrace)
                    End Try
                Next
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message & ex.StackTrace)
        End Try
    End Sub

    Private Sub pnlList_QueryContinueDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryContinueDragEventArgs) Handles pnlList.QueryContinueDrag

    End Sub

    Private Sub pnlList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlList.MouseDown
        Try
            If Items.Count > 0 Then
                For i As Integer = 0 To Me.Items.Count - 1
                    If e.Y + YScroll.Value >= RealItemPosition(i) And e.Y + YScroll.Value <= RealItemPosition(i) + RealItemHeight(i) Then
                        Me.SelectedIndex = i
                        Exit For
                    End If
                Next
            End If
            Invalidate()
        Catch ex As Exception
            Console.WriteLine(ex.Message & ex.StackTrace)
        End Try
    End Sub

    Private Sub pnlList_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlList.MouseMove
        If e.Button = MouseButtons.Right Then
            Dim DestOffsetY = YScroll.Value + ((((MousePosY - e.Y) * 0.2) \ 1) * 1)
            If DestOffsetY < 0 Then DestOffsetY = 0
            If DestOffsetY > MaxY Then DestOffsetY = MaxY
            YScroll.Value = DestOffsetY
        End If
    End Sub

    Private Sub pnlList_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pnlList.MouseWheel
        Dim DestOffsetY = YScroll.Value - e.Delta
        If DestOffsetY < 0 Then DestOffsetY = 0
        If DestOffsetY > MaxY Then DestOffsetY = MaxY
        YScroll.Value = DestOffsetY
    End Sub

    Private Sub YScroll_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles YScroll.Scroll
        Invalidate()
    End Sub

    Private Sub Sumbox_Invalidated(ByVal sender As Object, ByVal e As System.Windows.Forms.InvalidateEventArgs) Handles MyBase.Invalidated
        Me.pnlList.Invalidate()
    End Sub
End Class
